import { useState } from 'react'
import { getproductFabricTypeList } from '@/api/goods'
export interface IFabricTypeType {
  fabricTypeCode: string
  fabricTypeName: string
  fabricTypeId: string
}
const useSearchFabricType = (Key: 'fabricTypeCode' | 'fabricTypeId' = 'fabricTypeCode') => {
  let FabricTypeTimeout: ITimeoutType
  const [FabricTypeOptions, setFabricTypeOptions] = useState<IOptionItem[]>([])
  const handleSearchFabricType = (newValue = '') => {
    if (FabricTypeTimeout) {
      clearTimeout(FabricTypeTimeout)
      FabricTypeTimeout = null
    }
    const getOptions = async () => {
      const res = await getproductFabricTypeList<IFabricTypeType>({
        keyword: newValue,
        enableStatus: 1,
      })
      if (res.code === 200) {
        const data =
          res?.data?.list?.map((item) => {
            return {
              label: item.fabricTypeCode + '-' + item.fabricTypeName,
              value: item[Key],
              code: item.fabricTypeCode,
              id: item.fabricTypeId,
            }
          }) || []
        setFabricTypeOptions(data)
      }
    }
    FabricTypeTimeout = setTimeout(getOptions, 500)
  }
  return { FabricTypeOptions, setFabricTypeOptions, handleSearchFabricType }
}

export default useSearchFabricType
